package elijah.base.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.common.base.Objects;

import java.util.concurrent.TimeUnit;

import elijah.base.R;
import elijah.base.binding.TextBindable;
import elijah.base.databinding.WidgetAutoVerticalScrollTextBinding;
import elijah.base.thread.Threads;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;


public class AutoVerticalScrollText extends BaseWidget<WidgetAutoVerticalScrollTextBinding> implements TextBindable {
    private Disposable scrollDisposable;

    public int direction = 1;

    public AutoVerticalScrollText(@NonNull Context context) {
        super(context);
    }

    public AutoVerticalScrollText(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public void initView(@Nullable AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.AutoVerticalScrollText);
            binding.textView.setText(typedArray.getString(R.styleable.AutoVerticalScrollText_text));
            binding.textView.setTextColor(typedArray.getColor(R.styleable.AutoVerticalScrollText_textColor, binding.textView.getCurrentTextColor()));
            binding.textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, typedArray.getDimension(R.styleable.AutoVerticalScrollText_textSize, binding.textView.getTextSize()));
            typedArray.recycle();
        }

    }


    public void setText(String text) {
        if (Objects.equal(text, binding.textView.getText().toString())) {
            return;
        }
        binding.textView.setText(text);
        binding.scrollView.smoothScrollTo(0, 0);
        startAutoScroll();
    }

    public String getText() {
        return binding.textView.getText().toString();
    }

    public void startAutoScroll() {

        stopAutoScroll();
        scrollDisposable = Observable.interval(300, 150, TimeUnit.MILLISECONDS)
                .compose(Threads.io2main())
                .subscribe(aLong -> {
                    if (binding.textView.getHeight() <= binding.scrollView.getHeight()) {
                        return;
                    }
                    if ((direction == -1 && binding.scrollView.getScrollY() == 0) || (direction == 1 && binding.textView.getHeight() - binding.scrollView.getHeight()
                            == binding.scrollView.getScrollY())) {
                        direction = direction * -1;
                    }
                    binding.scrollView.smoothScrollBy(0, direction);

                });


    }


    private void stopAutoScroll() {
        if (scrollDisposable != null) {
            scrollDisposable.dispose();
            scrollDisposable = null;
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startAutoScroll();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopAutoScroll();
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        startAutoScroll();
        return super.dispatchKeyEvent(event);
    }
}